package lumis.portal.presentation.perspective.admin;

import lumis.portal.PortalException;
import lumis.portal.PortalRequestParameters;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.channel.ChannelConfig;
import lumis.portal.localization.ILocalizationManager;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.page.PageConfig;
import lumis.portal.presentation.core.HTMLFragmentComponent;
import lumis.portal.presentation.core.LayoutFilePage;
import lumis.portal.transaction.PortalTransactionFactory;
import lumis.util.IResource;
import lumis.util.ITransaction;

import java.util.List;

import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.util.string.AppendingStringBuffer;

/**
 * Renders a body footer specific for the Portal Studio.
 *
 * @version $Revision: 13321 $ $Date: 2011-08-08 11:32:13 -0300 (Mon, 08 Aug 2011) $
 * @since 6.0.0
 */
@SuppressWarnings("serial")
public class RenderPortalStudioFooterComponent extends HTMLFragmentComponent
{
	/**
	 * Creates a new RenderPortalStudioFooterComponent.
	 * @param id the component identifier.
	 * @param autoRenderBodyOnly if {@link #setRenderBodyOnly(boolean)} should
	 * automatically be set in case of layout file tags are to be stripped.
	 * @since 6.0.0
	 */
	public RenderPortalStudioFooterComponent(String id, boolean autoRenderBodyOnly)
	{
		super(id, autoRenderBodyOnly);
	}

	@Override
	protected CharSequence getHTMLFragment(MarkupStream markupStream, ComponentTag openTag)
	{
		try
		{
			LayoutFilePage page = (LayoutFilePage) getPage();
			IResource resource = PortalAdminStringResource.getInstance();
			SessionConfig sessionConfig = SessionConfig.getCurrentSessionConfig();
			
			AppendingStringBuffer bodyFooter = new AppendingStringBuffer(3500);
			
			ILocalizationManager localizationManager = ManagerFactory.getLocalizationManager();
			
			bodyFooter.append("<div id=\"LumisAdminNavigationPane\" class=\"cLumAdminNavigationPane cLumAdminReset\" >");
			bodyFooter.append("<div class=\"cLumAdminNavigationPaneTop\"></div>");

			// portal navigation
			if (page.getRootChannelId().equals(ChannelConfig.LUMIS_ADMIN_CHANNEL_ID))
			{
				bodyFooter.append("<div id=\"LumisAdminNavigationMenu\" id=\"cLumAdminNavigationMenu\">");

				bodyFooter.append("<div id=\"LumisAdminMenu0Left\"></div>");
				if (page.getParentChannelIds().size() >= 3)
				{
					String menuRootChannel = page.getParentChannelIds().get(2);
					bodyFooter.append(renderAdministrationMenu(page, menuRootChannel, -1, PortalTransactionFactory.getCurrentTransaction()));
				}

				bodyFooter.append("</div>");
			}
			else
			{

				bodyFooter.append("<div id=\"LumisAdminNavigationHeader\" class=\"cLumAdminNavigationHeader\">");
				bodyFooter.append(localizationManager.localize("STR_NAVIGATOR", PortalAdminStringResource.getInstance(), page.getLocale()));
				bodyFooter.append("</div>");
				bodyFooter.append("<div id=\"LumisAdminChannelTree\" class=\"cLumAdminChannelTree\"></div>");
				bodyFooter.append("<div id=\"LumisAdminServiceInterfaceListHeader\"  class=\"cLumAdminServiceInterfaceListHeader\"  style=\"display:none;\"></div>" + "<div id=\"LumisAdminServiceInterfaceList\" class=\"cLumAdminServiceInterfaceList\" style=\"display:none;\"></div>");
			}

			// render the administration bar
			bodyFooter.append("<div id=\"LumisAdminChannel\"  class=\"cLumAdminChannel\">");
			bodyFooter.append("<div>");
			bodyFooter.append("<a href=\""+PageConfig.PAGE_MAIN+"?"+PortalRequestParameters.PAGE_PARAMETER_PAGEID+"=LumisServices&"+PortalRequestParameters.PAGE_PARAMETER_CHANNELID+"=" + ChannelConfig.PORTAL_CHANNEL_ID + "\" >");
			bodyFooter.append("<img  src=\"lumis/portal/client/images/portal.gif\" alt=\" \" />");
			bodyFooter.append("PORTAL STUDIO");
			bodyFooter.append("</a>");
			bodyFooter.append("</div>");

			List<String> administrationChannelIds = ManagerFactory.getPresentationManager().getAdministrationBarChannelIds(sessionConfig, PortalTransactionFactory.getCurrentTransaction());

			for (String administrationChannelId : administrationChannelIds)
			{
				ChannelConfig adminChannelConfig = ManagerFactory.getChannelManager().get(sessionConfig, administrationChannelId, PortalTransactionFactory.getCurrentTransaction());
				String localizedName = localizationManager.localize(adminChannelConfig.getName(), resource, page.getLocale());
				// attribute, or use its name as class as below
				bodyFooter.append("<div>");
				bodyFooter.append("<a href=\""+PageConfig.PAGE_MAIN+"?"+PortalRequestParameters.PAGE_PARAMETER_CHANNELID+"=" + adminChannelConfig.getId() + "\" >");
				bodyFooter.append("<img  src=\"lumis/portal/client/images/" + adminChannelConfig.getName().toLowerCase() + ".gif\" alt=\" \" />");
				bodyFooter.append(localizedName);
				bodyFooter.append("</a>");
				bodyFooter.append("</div>");
			}

			bodyFooter.append("</div>");
			bodyFooter.append("</div>");
			
			return bodyFooter;
		}
		catch (PortalException e)
		{
			throw new RuntimeException(e);
		}
	}
	
	protected AppendingStringBuffer renderAdministrationMenu(LayoutFilePage page, String curChannelId, int iLevel, ITransaction transaction) throws PortalException
	{
		AppendingStringBuffer result = new AppendingStringBuffer(1000);
		SessionConfig sessionConfig = SessionConfig.getCurrentSessionConfig();
		PageConfig pageConfig = page.getPageWebResource().getPage();
		IResource resource = PortalAdminStringResource.getInstance();
		
		ChannelConfig curChannelConfig = ManagerFactory.getChannelManager().get(sessionConfig, curChannelId, transaction);
		if (!curChannelConfig.isHidden())
		{
			List<String> pageIds = ManagerFactory.getPageManager().getIdsByParentChannelId(sessionConfig, curChannelId, transaction);
			List<String> subChannelIds = ManagerFactory.getChannelManager().getIdsByParentChannelId(sessionConfig, curChannelId, transaction);

			for (String pageId : pageIds)
			{
				PageConfig curPageConfig = ManagerFactory.getPageManager().get(sessionConfig, pageId, transaction);
				if (!curPageConfig.isHidden() && !curPageConfig.isTemplate())
				{
					result.append("<div id=\"LumisAdminMenu" + (iLevel + 1) + "\"");
					if (curPageConfig.getId().equals(pageConfig.getId()))
						result.append(" class=\"cLumAdminMenuSelected\"");
					result.append("><a href=\"" + PageConfig.PAGE_MAIN + "?"+PortalRequestParameters.PAGE_PARAMETER_PAGEID+"=" + curPageConfig.getId() + "\">" + ManagerFactory.getLocalizationManager().localize(curPageConfig.getName(), resource, sessionConfig.getLocale()) + "</a></div>");
				}
			}

			for (String subChannelId : subChannelIds)
				result.append(renderAdministrationMenu(page, subChannelId, iLevel + 1, transaction));

			if (iLevel >= 0)
			{
				if (result.length() == 0)
				{
					boolean selected = isChannelInPath(curChannelConfig.getId(), page.getPageWebResource().getPage(), page.getParentChannelIds());
					String thisChannelHTML = "<div id=\"LumisAdminMenu" + iLevel + "\"";

					if (selected)
						thisChannelHTML += " class=\"cLumAdminMenuSelected\"";
					thisChannelHTML += "><a href=\"" + PageConfig.PAGE_MAIN + "?"+PortalRequestParameters.PAGE_PARAMETER_CHANNELID+"=" + curChannelConfig.getId() + "\">" + ManagerFactory.getLocalizationManager().localize(curChannelConfig.getName(), resource, sessionConfig.getLocale()) + "</a></div>";
					result.insert(0, thisChannelHTML);
				}
				else
				{
					String thisChannelHTML;
					if (iLevel == 0)
					{
						boolean selected = isChannelInPath(curChannelConfig.getId(), page.getPageWebResource().getPage(), page.getParentChannelIds());
						thisChannelHTML = "<div style=\"white-space: nowrap\" id=\"LumisAdminMenu0\"  onclick=\"LumisPortalAdmin.onToggleAdminMenu(this);\" ><img name=\"arrow\" id=\"arrow\" src=\"lumis/portal/client/images/";
						if (selected)
							thisChannelHTML += "AdminMenuOpen.gif";
						else
							thisChannelHTML += "AdminMenuClosed.gif";
						thisChannelHTML += "\" style=\"vertical-align: middle;\" alt=\" \"></image><a href=\"#\">" + ManagerFactory.getLocalizationManager().localize(curChannelConfig.getName(), resource, sessionConfig.getLocale()) + "</a></div>";

						if (!selected)
							result.insert(0, "<div style=\"display:none\">");
						else
							result.insert(0, "<div>");
						result.append("</div>");
					}
					else
					{
						thisChannelHTML = "<div id=\"LumisAdminMenu" + iLevel + "\"><a href=\"#\">" + ManagerFactory.getLocalizationManager().localize(curChannelConfig.getName(), resource, sessionConfig.getLocale()) + "</a></div>";
					}

					result.insert(0, thisChannelHTML);
				}
			}
		}

		return result;
	}

	protected boolean isChannelInPath(String channelId, PageConfig pageConfig, List<String> parentChannelIds)
	{
		if (pageConfig.getChannelId().equals(channelId))
			return true;

		for (String curChannelId : parentChannelIds)
		{
			if (curChannelId.equals(channelId))
			{
				return true;
			}
		}
		return false;
	}
}
